using System;
using System.Management;
using System.Runtime.InteropServices;

namespace WIT.Global;

/// <summary>
/// 硬件信息帮助类
/// </summary>
public class HardwareInfoHelper
{
	/// <summary>
	///
	/// </summary>
	public enum NCBCONST
	{
		/// <summary>
		/// absolute length of a net name
		/// </summary>
		NCBNAMSZ = 16,
		/// <summary>
		/// lana's in range 0 to MAX_LANA inclusive
		/// </summary>
		MAX_LANA = 254,
		/// <summary>
		/// NCB ENUMERATE LANA NUMBERS
		/// </summary>
		NCBENUM = 55,
		/// <summary>
		/// good return
		/// </summary>
		NRC_GOODRET = 0,
		/// <summary>
		/// NCB RESET
		/// </summary>
		NCBRESET = 50,
		/// <summary>
		/// NCB ADAPTER STATUS
		/// </summary>
		NCBASTAT = 51,
		/// <summary>
		/// Number of NAME's BUFFER
		/// </summary>
		NUM_NAMEBUF = 30
	}

	/// <summary>
	///
	/// </summary>
	public struct ADAPTER_STATUS
	{
		/// <summary>
		///
		/// </summary>
		[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
		public byte[] adapter_address;

		/// <summary>
		///
		/// </summary>
		public byte rev_major;

		/// <summary>
		///
		/// </summary>
		public byte reserved0;

		/// <summary>
		///
		/// </summary>
		public byte adapter_type;

		/// <summary>
		///
		/// </summary>
		public byte rev_minor;

		/// <summary>
		///
		/// </summary>
		public ushort duration;

		/// <summary>
		///
		/// </summary>
		public ushort frmr_recv;

		/// <summary>
		///
		/// </summary>
		public ushort frmr_xmit;

		/// <summary>
		///
		/// </summary>
		public ushort iframe_recv_err;

		/// <summary>
		///
		/// </summary>
		public ushort xmit_aborts;

		/// <summary>
		///
		/// </summary>
		public uint xmit_success;

		/// <summary>
		///
		/// </summary>
		public uint recv_success;

		/// <summary>
		///
		/// </summary>
		public ushort iframe_xmit_err;

		/// <summary>
		///
		/// </summary>
		public ushort recv_buff_unavail;

		/// <summary>
		///
		/// </summary>
		/// <summary>
		///
		/// </summary>
		public ushort t1_timeouts;

		/// <summary>
		///
		/// </summary>
		public ushort ti_timeouts;

		/// <summary>
		///
		/// </summary>
		public uint reserved1;

		/// <summary>
		///
		/// </summary>
		public ushort free_ncbs;

		/// <summary>
		///
		/// </summary>
		public ushort max_cfg_ncbs;

		/// <summary>
		///
		/// </summary>
		public ushort max_ncbs;

		/// <summary>
		///
		/// </summary>
		public ushort xmit_buf_unavail;

		/// <summary>
		///
		/// </summary>
		public ushort max_dgram_size;

		/// <summary>
		///
		/// </summary>
		public ushort pending_sess;

		/// <summary>
		///
		/// </summary>
		public ushort max_cfg_sess;

		/// <summary>
		///
		/// </summary>
		public ushort max_sess;

		/// <summary>
		///
		/// </summary>
		public ushort max_sess_pkt_size;

		/// <summary>
		///
		/// </summary>
		public ushort name_count;
	}

	/// <summary>
	///
	/// </summary>
	public struct NAME_BUFFER
	{
		/// <summary>
		///
		/// </summary>
		[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
		public byte[] name;

		/// <summary>
		///
		/// </summary>
		public byte name_num;

		/// <summary>
		///
		/// </summary>
		public byte name_flags;
	}

	/// <summary>
	///
	/// </summary>
	public struct NCB
	{
		/// <summary>
		///
		/// </summary>
		public byte ncb_command;

		/// <summary>
		///
		/// </summary>
		public byte ncb_retcode;

		/// <summary>
		///
		/// </summary>
		public byte ncb_lsn;

		/// <summary>
		///
		/// </summary>
		public byte ncb_num;

		/// <summary>
		///
		/// </summary>
		public IntPtr ncb_buffer;

		/// <summary>
		///
		/// </summary>
		public ushort ncb_length;

		/// <summary>
		///
		/// </summary>
		[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
		public byte[] ncb_callname;

		/// <summary>
		///
		/// </summary>
		[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
		public byte[] ncb_name;

		/// <summary>
		///
		/// </summary>
		public byte ncb_rto;

		/// <summary>
		///
		/// </summary>
		public byte ncb_sto;

		/// <summary>
		///
		/// </summary>
		public IntPtr ncb_post;

		/// <summary>
		///
		/// </summary>
		public byte ncb_lana_num;

		/// <summary>
		///
		/// </summary>
		public byte ncb_cmd_cplt;

		/// <summary>
		///
		/// </summary>
		[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
		public byte[] ncb_reserve;

		/// <summary>
		///
		/// </summary>
		public IntPtr ncb_event;
	}

	/// <summary>
	///
	/// </summary>
	public struct LANA_ENUM
	{
		/// <summary>
		///
		/// </summary>
		public byte length;

		/// <summary>
		///
		/// </summary>
		[MarshalAs(UnmanagedType.ByValArray, SizeConst = 254)]
		public byte[] lana;
	}

	/// <summary>
	///
	/// </summary>
	[StructLayout(LayoutKind.Auto)]
	public struct ASTAT
	{
		/// <summary>
		///
		/// </summary>
		public ADAPTER_STATUS adapt;

		/// <summary>
		///
		/// </summary>
		[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
		public NAME_BUFFER[] NameBuff;
	}

	/// <summary>
	///
	/// </summary>
	public class Win32API
	{
		/// <summary>
		///
		/// </summary>
		/// <param name="ncb"></param>
		/// <returns></returns>
		[DllImport("NETAPI32.DLL")]
		public static extern char Netbios(ref NCB ncb);
	}

	/// <summary>
	/// 获取系统UUID
	/// </summary>
	/// <returns></returns>
	public static string GetSysID()
	{
		try
		{
			ManagementObjectCollection instances = new ManagementClass("Win32_ComputerSystemProduct").GetInstances();
			string result = null;
			using (ManagementObjectCollection.ManagementObjectEnumerator managementObjectEnumerator = instances.GetEnumerator())
			{
				if (managementObjectEnumerator.MoveNext())
				{
					result = ((ManagementObject)managementObjectEnumerator.Current).Properties["UUID"].Value.ToString();
				}
			}
			return result;
		}
		catch
		{
			return string.Empty;
		}
	}

	/// <summary>
	/// 获取CPU ID
	/// </summary>
	/// <returns></returns>
	public static string GetCpuID()
	{
		try
		{
			ManagementObjectCollection instances = new ManagementClass("Win32_Processor").GetInstances();
			string result = null;
			using (ManagementObjectCollection.ManagementObjectEnumerator managementObjectEnumerator = instances.GetEnumerator())
			{
				if (managementObjectEnumerator.MoveNext())
				{
					result = ((ManagementObject)managementObjectEnumerator.Current).Properties["ProcessorId"].Value.ToString();
				}
			}
			return result;
		}
		catch
		{
			return string.Empty;
		}
	}

	/// <summary>
	/// 获取主板序列号
	/// </summary>
	/// <returns></returns>
	public static string GetMainboardID()
	{
		try
		{
			ManagementObjectCollection instances = new ManagementClass("Win32_BaseBoard").GetInstances();
			string result = null;
			using (ManagementObjectCollection.ManagementObjectEnumerator managementObjectEnumerator = instances.GetEnumerator())
			{
				if (managementObjectEnumerator.MoveNext())
				{
					result = ((ManagementObject)managementObjectEnumerator.Current).Properties["SerialNumber"].Value.ToString();
				}
			}
			return result;
		}
		catch
		{
			return string.Empty;
		}
	}

	/// <summary>
	/// 获取第一块硬盘ID
	/// </summary>
	/// <returns></returns>
	public static string GetHardDiskID()
	{
		try
		{
			ManagementObjectCollection instances = new ManagementClass("Win32_PhysicalMedia").GetInstances();
			string result = null;
			using (ManagementObjectCollection.ManagementObjectEnumerator managementObjectEnumerator = instances.GetEnumerator())
			{
				if (managementObjectEnumerator.MoveNext())
				{
					result = ((ManagementObject)managementObjectEnumerator.Current).Properties["SerialNumber"].Value.ToString().Trim();
				}
			}
			return result;
		}
		catch
		{
			return string.Empty;
		}
	}

	/// <summary>
	/// 获取网卡MAC地址
	/// </summary>
	/// <returns></returns>
	public static string GetMacAddress()
	{
		string text = string.Empty;
		try
		{
			NCB ncb = default(NCB);
			ncb.ncb_command = 55;
			int num = Marshal.SizeOf(typeof(LANA_ENUM));
			ncb.ncb_buffer = Marshal.AllocHGlobal(num);
			ncb.ncb_length = (ushort)num;
			char num2 = Win32API.Netbios(ref ncb);
			LANA_ENUM lANA_ENUM = (LANA_ENUM)Marshal.PtrToStructure(ncb.ncb_buffer, typeof(LANA_ENUM));
			Marshal.FreeHGlobal(ncb.ncb_buffer);
			if (num2 != 0)
			{
				return string.Empty;
			}
			ASTAT aSTAT = default(ASTAT);
			for (int i = 0; i < lANA_ENUM.length; i++)
			{
				ncb.ncb_command = 50;
				ncb.ncb_lana_num = lANA_ENUM.lana[i];
				if (Win32API.Netbios(ref ncb) != 0)
				{
					return string.Empty;
				}
				ncb.ncb_command = 51;
				ncb.ncb_lana_num = lANA_ENUM.lana[i];
				ncb.ncb_callname[0] = 42;
				num = Marshal.SizeOf(typeof(ADAPTER_STATUS)) + Marshal.SizeOf(typeof(NAME_BUFFER)) * 30;
				ncb.ncb_buffer = Marshal.AllocHGlobal(num);
				ncb.ncb_length = (ushort)num;
				char num3 = Win32API.Netbios(ref ncb);
				aSTAT.adapt = (ADAPTER_STATUS)Marshal.PtrToStructure(ncb.ncb_buffer, typeof(ADAPTER_STATUS));
				Marshal.FreeHGlobal(ncb.ncb_buffer);
				if (num3 == '\0')
				{
					if (i > 0)
					{
						text += ":";
					}
					text = string.Format("{0,2:X}{1,2:X}{2,2:X}{3,2:X}{4,2:X}{5,2:X}", aSTAT.adapt.adapter_address[0], aSTAT.adapt.adapter_address[1], aSTAT.adapt.adapter_address[2], aSTAT.adapt.adapter_address[3], aSTAT.adapt.adapter_address[4], aSTAT.adapt.adapter_address[5]);
				}
			}
		}
		catch
		{
		}
		return text.Replace(' ', '0');
	}
}
